package org.nmgyjt.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.equipage.Equipage;
import org.nmgyjt.springboot.entity.materials.Materials;
import org.nmgyjt.springboot.entity.rescueTeam.RescueTeam;
import org.nmgyjt.springboot.entity.storeInfo.StoreInfo;
import org.nmgyjt.springboot.mapper.EquipageMapper;
import org.nmgyjt.springboot.mapper.MaterialsMapper;
import org.nmgyjt.springboot.mapper.RescueTeamMapper;
import org.nmgyjt.springboot.mapper.StoreMapper;
import org.nmgyjt.springboot.service.EquipageService;
import org.nmgyjt.springboot.service.MaterialsService;
import org.nmgyjt.springboot.service.RescueTeamService;
import org.nmgyjt.springboot.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class MaterialsServiceImpl extends ServiceImpl<MaterialsMapper, Materials> implements MaterialsService {
    @Autowired
    private RescueTeamMapper rescueTeamMapper;
    @Autowired
    private EquipageMapper equipageMapper;
    @Autowired
    private RescueTeamService rescueTeamService;
    @Autowired
    private EquipageService equipageService;

    @Override
    public RescueTeam getByDepId(String depId) {
        QueryWrapper<RescueTeam> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("dep_id", depId);
        RescueTeam rescueTeam = rescueTeamMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isEmpty(rescueTeam)) {
            QueryWrapper<Equipage> equipageQueryWrapper = new QueryWrapper<>();
            equipageQueryWrapper.eq("rescue_team_id", rescueTeam.getId());
            List<Equipage> equipageList = equipageMapper.selectList(equipageQueryWrapper);
            rescueTeam.setEquipageList(equipageList);
        }
        return rescueTeam;
    }

    @Override
    public ApiRes insertOrUpdate(Materials materials) {
//        try {
//            List<Equipage> equipageList = rescueTeam.getEquipageList();
//            rescueTeamService.saveOrUpdate(rescueTeam);
//            List<Long> ids = new ArrayList<>();
//            if (!ObjectUtils.isEmpty(rescueTeam.getId()) && equipageList.size() > 0) {
//                for (Equipage equipage : equipageList) {
//                    if (!ObjectUtils.isEmpty(equipage.getId())) {
//                        ids.add(equipage.getId());
//                    }
//                }
//                //删除前端未传输的装备id数据
//                QueryWrapper<Equipage> equipageQueryWrapper = new QueryWrapper<>();
//                equipageQueryWrapper.eq("rescue_team_id", rescueTeam.getId());
//                if (ids.size() > 0) {
//                    equipageQueryWrapper.notIn("id", ids);
//                }
//                equipageMapper.delete(equipageQueryWrapper);
//
//                equipageService.saveOrUpdateBatch(equipageList);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ApiRes.customFail("保存失败！");
//        }
        return ApiRes.ok();
    }
}
